//
//  ReportAPI.swift
//  kano
//
//  Created by Frank on 2024/10/8.
//

import Foundation
import Moya
import KakaJSON

import Alamofire

struct JSONArrayEncoding: ParameterEncoding {
    static let `default` = JSONArrayEncoding()

    func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest {
        var request = try urlRequest.asURLRequest()

        guard let json = parameters?["jsonArray"] else {
            return request
        }

        let data = try JSONSerialization.data(withJSONObject: json, options: [])

//        if request.value(forHTTPHeaderField: "Content-Type") == nil {
//            request.setValue("application/json", forHTTPHeaderField: "Content-Type")
//        }
        
        request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
        request.setValue(UIDevice.ka_UUID.toBase64(), forHTTPHeaderField: "SW-K-ID")
        

        request.httpBody = data

        return request
    }
}

enum ReportAPI {
    case report(_ parameters: [[String: Any]] = [])
    case subchannel(_ parameters: Dictionary<String, Any>? = nil)
}

extension ReportAPI : TargetType {
    var baseURL: URL {
        return URL(string: NetConfig.readBaseUrl())!
    }
    
    var path: String {
        switch self {
        case .report:
            let path = NetConfig.fullReport(path: "")
            return path
        case .subchannel:
            let path = NetConfig.fullNormal(path: "/getUserPicture")
            return path
            
        default:
            return ""
        }
    }
    
    var method: Moya.Method {
        switch self {
        default:
            return .post
        }
    }
    
    var task: Moya.Task {
        switch self {
        case .report(let parameters):
            if kisEncrypt == 1 {
                let data: Dictionary<String, Any>
                if (parameters.count == 0) {
                    data = ["body" : ""]
                } else {
                    let bodyData = KAEncrypt.ka_encrypt(parameters.jsonString()!)
                    data = ["body" : bodyData]
                }
                return .requestCompositeParameters(bodyParameters: data as [String : Any], bodyEncoding: JSONEncoding.default, urlParameters: NetConfig.urlParam())
            } else {
//                return .requestCompositeParameters(bodyParameters:["jsonArray": parameters], bodyEncoding: JSONArrayEncoding.default, urlParameters: NetConfig.urlParam())
//                return .requestParameters(parameters: ["jsonArray": parameters], encoding: JSONArrayEncoding.default)

                
                do {
                    let jsonData = try JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted)
                    print("Data: \(jsonData)")
                    return .requestCompositeData(bodyData: jsonData, urlParameters: NetConfig.urlParam())
                } catch {
                    return .requestPlain
                }
            }
        case .subchannel(let parameters):
            if kisEncrypt == 1 {
                let data: Dictionary<String, Any>
                if (parameters == nil) {
                    data = ["body" : ""]
                } else {
                    let bodyData = KAEncrypt.ka_encrypt(parameters!.jsonString()!)
                    data = ["body" : bodyData]
                }
                return .requestCompositeParameters(bodyParameters: data as [String : Any], bodyEncoding: JSONEncoding.default, urlParameters: NetConfig.urlParam())
            } else {
                return .requestCompositeParameters(bodyParameters:parameters ?? ["" : ""], bodyEncoding: JSONEncoding.default, urlParameters: NetConfig.urlParam())
            }
        default:
            return .requestPlain
        }
    }
    
    var headers: [String : String]? {
        return ["Content-Type":"application/json; charset=utf-8",
                "SW-K-ID": UIDevice.ka_UUID.toBase64()]
    }
}

